package com.dzf.service.impl;


import com.dzf.entity.Book;
import com.dzf.mapper.BookMapper;
import com.dzf.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @author DZF
 */
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookMapper bookMapper;


    @Override
    public List<Book> findAll() {
        return bookMapper.findAll();

    }

    @Override
    public Book findById(int bookid) {
        return bookMapper.findById(bookid);
    }

    @Override
    public int findByTotal(Map<String, Object> map) {
        return bookMapper.findByTotal(map);
    }



    @Override
    public Map<String, Object> findByIf(int start, int num, String condition) {
        //计算起始行
         start = (start*num)-num;

        //找对象中对应的关键子给予样式
        List<Book> books = bookMapper.findIf(start, num, condition);
        for (Book book : books)
        {
            book.setAuthor(book.getAuthor().replace(condition,"<span style='color: red;'>"+condition+"</span>"));
            book.setName(book.getName().replace(condition,"<span style='color: red;'>"+condition+"</span>"));
            book.setPublisher(book.getPublisher().replace(condition,"<span style='color: red;'>"+condition+"</span>"));
        }


        Map<String, Object> map = new HashMap<>();
        map.put("condition",condition);


        Map<String, Object> map1 = new HashMap<>();
        map1.put("total",bookMapper.findByTotal(map));
        map1.put("books",books);
        return map1;
    }

    @Transactional
    @Override
    public void amend(Book book) {
        if (book.getId()==0)
        {
            bookMapper.bookAdd(book);
        }else{
            bookMapper.bookUpd(book);
        }
    }

    @Cacheable(value = "book",keyGenerator = "springCacheDefaultKeyGenerator",cacheManager = "cacheManager1Hour")
    @Transactional
    @Override
    public Map<String, Object> findBookByPage(int start, int num, int typeid) {
        start=(start*num)-num;

        //把条件放入
        Map<String, Object> map2 = new HashMap<>();
        map2.put("typeid",typeid);

        //获得Total总数 和 商品数据
        int total = bookMapper.findByTotal(map2);
        List<Book> books = bookMapper.findBookByPage(start, num, typeid);

        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("books",books);
        return map;
    }

    /**
     * 点赞功能，没有实际去实现，可以无限点赞
     * @param bookid 书本·ID
     */
    @Override
    public void addRate(int bookid) {
        bookMapper.addRate(bookid);
    }


    /**
     * 动态的条件分页
     * @param start 起始页数
     * @param num   每页商品
     * @param map   条件
     * @return      查询后的商品
     */
    @Cacheable(value = "book",keyGenerator = "springCacheDefaultKeyGenerator",cacheManager = "cacheManager1Hour")
    @Transactional
    @Override
    public Map<String , Object> findBookByPageOrd(int start, int num, Map<String, Object> map) {
        //查询条件由谁调用此方法决定
        int total = bookMapper.findByTotal(map);
        List<Book> books = bookMapper.findBookByPageOrd(start, num, map);
        Map<String , Object> map1 = new HashMap<>();
        map1.put("total",total);
        map1.put("books",books);
        return map1;
    }

    /*
    * 被重写的方法
    *
    * */
    @Cacheable(value = "book",keyGenerator = "springCacheDefaultKeyGenerator",cacheManager = "cacheManager1Hour")
    @Override
    public List<Book> findOrd5(String cdt) {
        return bookMapper.findOrd5(cdt);
    }

    @Cacheable(value = "book",keyGenerator = "springCacheDefaultKeyGenerator",cacheManager = "cacheManager1Hour")
    @Override
    public List<Book> findRate5() {
        return bookMapper.findRate5();
    }

    @Cacheable(value = "book",keyGenerator = "springCacheDefaultKeyGenerator",cacheManager = "cacheManager1Hour")
    @Override
    public List<Book> findSale5() {
        return bookMapper.findSale5();
    }

    @Cacheable(value = "book",keyGenerator = "springCacheDefaultKeyGenerator",cacheManager = "cacheManager1Hour")
    @Override
    public List<Book> findFav5() {
        return bookMapper.findFav5();
    }
}
